import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author John
 * @version 1.0
 * @date 2012-5-24
 */
public class MobileSecretaryServer {

	private static Connection con;
	private static Statement sql;
	private static ServerSocket ss;

	public static void main(String[] args) {
		if (args.length < 2) {
			System.out.println("参数不正确！");
			System.exit(2);
		}
		String strPort = args[0];
		final int port = Integer.parseInt(strPort);
		String dbPath = args[1];

		String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + dbPath;

		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return;
		}
		try {
			Properties prop = new Properties();
			prop.put("charSet", "gbk");
			// 然后如果数据库有密码，我们必须把用户名和密码的prop写完整
			prop.put("user", "");
			prop.put("password", "");
			con = DriverManager.getConnection(url, prop);
			sql = con.createStatement();
			final ExecutorService pool = Executors.newCachedThreadPool();
			try {
				ss = new ServerSocket(port);
			} catch (IOException e) {
				e.printStackTrace();
				return;
			}
			Thread server = new Thread() {

				@Override
				public void run() {

					while (ss != null) {
						ServerSocket sss = ss;
						try {
							Socket s = sss.accept();
							ClientRunnable cr = new ClientRunnable(s, sql);
							pool.execute(cr);
						} catch (Exception e) {
							e.printStackTrace();
							if (ss != null) {
								try {
									ss.close();
								} catch (IOException e1) {
									e1.printStackTrace();
								}
							}
							break;
						}
					}
				}
			};

			server.start();
			while (server.isAlive()) {
				BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
				String s;
				try {
					while ((s = in.readLine()) != null){
					}
						
				} catch (IOException e) {
					e.printStackTrace();
					ss = null;
					try {
						server.join();
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}

		} catch (SQLException e1) {
			e1.printStackTrace();
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (sql != null) {
				try {
					sql.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
